package club.twelveblog.tblog.common.config;

import com.aliyun.oss.OSSClient;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

/**
 * 上传图片客户端
 */
@Data
public class AliOssClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(AliOssClient.class);

    private String accessKeyId;
    private String accessKeySecret;
    private String endpoint;


    /**
     * 上传某个Object
     *
     * @param bucketName
     * @param bucketUrl
     * @param inputStream
     * @return
     */
    public boolean putObject(String bucketName, String bucketUrl, InputStream inputStream) {
        OSSClient client = new OSSClient(this.endpoint, this.accessKeyId, this.accessKeySecret);
        try {
            // 上传Object.
            client.putObject(bucketName, bucketUrl, inputStream);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            client.shutdown();
        }
        return true;
    }


}
